package com.xjtu.chapter07.ex7_3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/19 10:45
 */
public class SudoGame {
    JFrame gameFrame = new JFrame("数独游戏");
    // Name-constants for the game properties
    public static final int GRID_SIZE = 9; // Size of the board
    public static final int SUBGRID_SIZE = 3; // Size of the sub-grid

    // Name-constants for UI control (sizes, colors and fonts)
    public static final int CELL_SIZE = 60; // Cell width/height in pixels
    public static final int CANVAS_WIDTH = CELL_SIZE * GRID_SIZE;
    public static final int CANVAS_HEIGHT = CELL_SIZE * GRID_SIZE; // Board width/height in pixels
    public static final Color OPEN_CELL_BGCOLOR = Color.YELLOW;
    public static final Color CLOSED_CELL_BGCOLOR = new Color(240, 240, 240);
    public static final Color CLOSED_CELL_TEXT = Color.BLACK;
    public static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20);

    //组装视图
    public void init() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
        gameFrame.setVisible(true);
        gameFrame.setLayout(new GridLayout(9, 9));

        //设置菜单栏
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenuItem itemNew = new JMenuItem("New Game");
        JMenuItem itemReset = new JMenuItem("Reset Game");
        JMenuItem itemExit = new JMenuItem("Exit");

        itemExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.exit(0);
            }
        });

        menu.add(itemNew);
        menu.add(itemReset);
        menu.add(itemExit);
        menuBar.add(menu);
        gameFrame.setJMenuBar(menuBar);


        JPanel gamePanel = new JPanel();
        JTextField[][] tfCells = new JTextField[9][9];
        for (int row = 0; row < GRID_SIZE; ++row) {
            for (int col = 0; col < GRID_SIZE; ++col) {
                tfCells[row][col] = new JTextField();      // Allocate element of array
                gamePanel.add(tfCells[row][col]);               // ContentPane adds JTextField
                tfCells[row][col].setText("1");
                tfCells[row][col].setEditable(false);
                tfCells[row][col].setBackground(CLOSED_CELL_BGCOLOR);
                tfCells[row][col].setForeground(CLOSED_CELL_TEXT);

                tfCells[row][col].setHorizontalAlignment(JTextField.CENTER);
                tfCells[row][col].setFont(FONT_NUMBERS);
            }
        }
        gamePanel.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
        JTextField statusBar = new JTextField();

        gameFrame.add(statusBar, BorderLayout.SOUTH);
        gameFrame.add(gamePanel);

        gameFrame.pack();
        gameFrame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            new SudoGame().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
